package controller;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import common.CommonUtil;
import common.GetPinyin;
import service.UserService;
import static filter.AuthticationFilter.SESS_USER;
import json.JSONParam;
import json.JSONResponse;
import json.JSONUser;
import vo.DataTableReturnObject;
import vo.User;

@Controller
@RequestMapping("/userInfo")
public class UserInfoController extends JSONController {
	@Resource
	private UserService userService;

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	@ResponseBody
	public JSONResponse login(@RequestBody
	Map<String, String> map, HttpSession session) {
		System.out.println(map);
		User user = userService.findUserByName(map.get("account"));
		System.err.println(user.getAccount());;
		if (!(user==null)) {
			if (highsoft.common.EncryptUtil.getEncryptString(map.get("password")).equals(user.getPassword())) {
				session.setAttribute(SESS_USER, user);
				System.err.println(successed(user));
				return successed(user);
			} else {
				return failed("用户名与口令不匹配");
			}
		} else {
			return failed("用户名不存在");
		}
	}
	
	@RequestMapping(value = "/reg", method = RequestMethod.POST)
	@ResponseBody
	public JSONResponse reg(@RequestBody Map<String, String> map, HttpSession session) {
		User user = userService.findUserByName(map.get("account"));
		if (user==null) {
			User regUser=new User();
			regUser.setAccount(map.get("account"));
			regUser.setName(map.get("name"));
			regUser.setName_py(GetPinyin.getPinYinHeadChar(map.get("name")));
			regUser.setPassword(highsoft.common.EncryptUtil.getEncryptString(map.get("password")));
			regUser.setNative_place(map.get("native_place"));
			regUser.setSex(map.get("sex"));
			regUser.setTel(map.get("tel"));
			regUser.setEmail(map.get("email"));		
			regUser.setIs_receive_email("Y");
			System.out.println(CommonUtil.getDateTime());
			System.out.println(CommonUtil.getDateTimeStr());
			regUser.setReg_time(CommonUtil.getDateTime());
			System.out.println(regUser);
			userService.save(regUser);
			session.setAttribute(SESS_USER, regUser);
			return successed(regUser);
		} else {
			return failed("警告：该用户已存在！");
		}
	}

	@RequestMapping(value = "/logout", method = RequestMethod.POST)
	@ResponseBody
	public JSONResponse logout(HttpSession session) {
		session.removeAttribute(SESS_USER);
		return successed(null);
	}

	@RequestMapping(value = "/getLoginUser", method = RequestMethod.POST)
	@ResponseBody
	public JSONResponse getLoginUser(HttpSession session) {
		Object o = session.getAttribute(SESS_USER);
		if (o != null) {
			return successed(o);
		} else {
			return failed(new HashSet());
		}
	}
	
	@RequestMapping(value = "/search", method = RequestMethod.POST)
	@ResponseBody
	public JSONResponse search(@RequestBody JSONParam[] params) throws IllegalAccessException, InvocationTargetException {
		HashMap<String, String> paramMap = convertToMap(params);
		String sEcho = paramMap.get("sEcho");
		String userName = paramMap.get("userName");
		int start = Integer.parseInt(paramMap.get("iDisplayStart"));
		int length = Integer.parseInt(paramMap.get("iDisplayLength"));
		System.out.println("userName:"+userName);
		List<Object> userList = userService.search("from vo.User where account like '%"+userName+"%'",new String[]{userName}, start, length);
		//List<Object> customerList=null;
		Long count = (Long)userList.get(0);
		int record = userList.size() - 1;
		String[][] data = new String[record][];
		for(int i=0; i<record; i++) {
			User user = (User)userList.get(i+1);
			JSONUser jsonUser = new JSONUser();
			BeanUtils.copyProperties(jsonUser, user);
			data[i] = jsonUser.toArray();
		}
		
		return successed(new DataTableReturnObject(count.longValue(), count.longValue(), sEcho, data));
	}
}